//
//  SearchViewController.m
//  Weiweiyunshi
//
//  Created by 王译 on 17/2/17.
//  Copyright © 2017年 王译. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchModel.h"
#import "ResultViewController.h"
#import "VVNavigationController.h"
@interface SearchViewController ()<PYSearchViewControllerDelegate>
{
    NSMutableArray *dataSource;
    ResultViewController *result;
}
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    self.searchBar.placeholder = @"添加好友";
    self.showSearchHistory  = NO;
    // Do any additional setup after loading the view.
}


/** 点击(开始)搜索时调用 */
- (void)searchViewController:(PYSearchViewController *)searchViewController didSearchWithsearchBar:(UISearchBar *)searchBar searchText:(NSString *)searchText{
    NSLog(@"%@",searchText);
    
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];

    
}




/** 点击搜索建议时调用，如果实现该代理方法则点击搜索建议时searchViewController:didSearchWithsearchBar:searchText:失效 */
- (void)searchViewController:(PYSearchViewController *)searchViewController didSelectSearchSuggestionAtIndex:(NSInteger)index searchText:(NSString *)searchText{
    NSLog(@"搜索建议:%ld,建议结果:%@",index,searchText);
    result = [[ResultViewController alloc]init];
    result.userId = [dataSource[index]userId];
    result.isFriend = [dataSource[index]isFriend];
    VVNavigationController *ResultVc = [[VVNavigationController alloc]initWithRootViewController:result];
    //    [self.navigationController pushViewController:ResultVc animated:YES];
    [self presentViewController:ResultVc animated:YES completion:nil];
}
/** 搜索框文本变化时，显示的搜索建议通过searchViewController的searchSuggestions赋值即可 */
- (void)searchViewController:(PYSearchViewController *)searchViewController  searchTextDidChange:(UISearchBar *)searchBar searchText:(NSString *)searchText{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *userId =  [userDefaults objectForKey:@"loginName"];
    if(searchText.length)
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
     
            NSString *urlStr = [NSString stringWithFormat:@"http://222.73.213.189:8088/SSIL/ContactFuzzySearch.do?searchValue=%@&userId=%@",searchText,userId];
            urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [WYHttpRequest getWithURLString:urlStr parameters:nil success:^(id responseObject) {
                dataSource = [NSMutableArray array];

                NSArray *arr = responseObject;
                for (id obj in arr) {
                    SearchModel *model = [SearchModel yy_modelWithJSON:obj];
                    [dataSource addObject:model];
                }
                // 返回
                NSMutableArray *searchSuggestionsM;
                searchSuggestionsM = [NSMutableArray array];
                for (id obj in dataSource) {
                    NSString *searchSuggestion = [obj username];
                    [searchSuggestionsM addObject:searchSuggestion];
                }
                searchViewController.searchSuggestions = searchSuggestionsM;
                // searchViewController.searchSuggestions = searchSuggestionsM;
            } failure:^(NSError *error) {
                
            }];
            
            
        });
    }
}
/** 点击取消时调用，如果没有实现该代理方法，默认执行：[self dismissViewControllerAnimated:YES completion:nil]; */
- (void)didClickCancel:(PYSearchViewController *)searchViewController{
    NSLog(@"取消");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
